﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sequence
{
    /// <summary>
    /// Interaction logic for SequenceStep.xaml
    /// </summary>
    public partial class SequenceStep : UserControl
    {
        public event EventHandler<SequenceStepMouseMoveEventArgs> OnMoveStart;
        public event EventHandler<SequenceStepMouseMoveEventArgs> OnMove;
        public event EventHandler<SequenceStepMouseMoveEventArgs> OnMoveEnd;
        public event EventHandler<EventArgs> OnRemoveStep;
        private Point _startDelta;
        private SalesManagerEntities _context;
        private int _ownerID;

        public SequenceStep(SalesManagerEntities context, int ownerID)
        {
            InitializeComponent();
            _context = context;
            _ownerID = ownerID;
        }

        private void btnRemoveStep_Click(object sender, RoutedEventArgs e)
        {
            tblSequenceStep ss = (tblSequenceStep)this.DataContext;
            while (ss.NextSteps.Count > 0)
                ss.NextSteps.First().PrevSequenceStep = ss.PrevSequenceStep;

            _context.DeleteObject(ss);
            if (OnRemoveStep != null)
                OnRemoveStep(this, new EventArgs());
        }

        private void btnDeletePRoduct_Click(object sender, RoutedEventArgs e)
        {
            tblSequenceStepProduct sequenceStepProduct = (tblSequenceStepProduct)((Button)e.Source).CommandParameter;
            _context.DeleteObject(sequenceStepProduct);
        }

        private void btnAddProduct_Click(object sender, RoutedEventArgs e)
        {
            DialogProductList dlg = new DialogProductList(_ownerID);
            if ((bool)dlg.ShowDialog())
            {
                tblSequenceStepProduct newSeqStepProduct = new tblSequenceStepProduct();
                newSeqStepProduct.Product = (tblProduct)_context.GetObjectByKey(new System.Data.EntityKey(
                    "SalesManagerEntities.tblProduct", "ProductID", dlg.ProductID));
                newSeqStepProduct.SequenceStep = (tblSequenceStep)this.DataContext;
            }
        }

        private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void Rectangle_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void Rectangle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            rect1.CaptureMouse();
            if (OnMoveStart != null)
                OnMoveStart(this, new SequenceStepMouseMoveEventArgs(e.GetPosition(this)));
        }

        private void Rectangle_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (rect1.IsMouseCaptured)
            {
                if (OnMoveEnd != null)
                    OnMoveEnd(this, new SequenceStepMouseMoveEventArgs(e.GetPosition(this)));
                rect1.ReleaseMouseCapture();
            }
        }

        private void rect1_MouseMove(object sender, MouseEventArgs e)
        {
            if (rect1.IsMouseCaptured)
            {
                if (OnMove != null)
                    OnMove(this, new SequenceStepMouseMoveEventArgs(e.GetPosition(this)));
            }
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            imgCorner.CaptureMouse();
            Point p = e.GetPosition(this);
            _startDelta = new Point(this.Width - p.X, this.Height - p.Y);
        }

        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (imgCorner.IsMouseCaptured)
                imgCorner.ReleaseMouseCapture();
        }

        private void Image_MouseMove(object sender, MouseEventArgs e)
        {
            if (imgCorner.IsMouseCaptured)
            {
                Point p = e.GetPosition(this);
                this.Width = p.X + _startDelta.X;
                this.Height = p.Y + _startDelta.Y;
            }
        }

        private void imgCorner_MouseEnter(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.SizeNWSE;
        }

        private void imgCorner_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
        }
    }

    public class SequenceStepMouseMoveEventArgs : EventArgs
    {
        public Point Pos;

        public SequenceStepMouseMoveEventArgs(Point pos)
        {
            Pos = pos;
        }
    }
}
